package com.qf.utils.http;

import com.qf.common.configure.HttpClientConfig;
import com.qf.common.configure.ProxyInfo;
import com.qf.common.utils.HttpClientUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

public class HttpCallCli implements Callable<String> {
    private String url;
    private String param;

    public HttpCallCli(String url, String param) {
        this.url = url;
        this.param = param;
    }

    @Override
    public String call() {

        List<NameValuePair> params = new LinkedList<>();
        params.add(new BasicNameValuePair("isHistory", "false"));
        params.add(new BasicNameValuePair("p$rq", "PubMedStaticPages.PubMedSP.ClinicalQueryC.ClinicalQuery:FetchClinicalStudy"));
        params.add(new BasicNameValuePair("p$site", "pubmed"));
        params.add(new BasicNameValuePair("cqTermComputed", param));

        ProxyInfo pi = new ProxyInfo();
//        pi.setTimeout(50000);
        HttpClientConfig config = new HttpClientConfig(pi, url, null, true);
        config.setNvpsList(params);
        String html = HttpClientUtil.post(config);
        return html;
    }

//    CloseableHttpClient client = HttpClients.custom().build();
//    String html = "";
//        try {
//        HttpPost httpPost = new HttpPost(url);
//        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0");
//        List<NameValuePair> params = new LinkedList<>();
//        params.add(new BasicNameValuePair("isHistory", "false"));
//        params.add(new BasicNameValuePair("p$rq", "PubMedStaticPages.PubMedSP.ClinicalQueryC.ClinicalQuery:FetchClinicalStudy"));
//        params.add(new BasicNameValuePair("p$site", "pubmed"));
//        params.add(new BasicNameValuePair("cqTermComputed", param));
//        // 使用URL实体转换工具
//        UrlEncodedFormEntity urlEntity = null;
//        try {
//            urlEntity = new UrlEncodedFormEntity(params, "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        httpPost.setEntity(urlEntity);
//        RequestConfig requestConfig = RequestConfig.custom()
//                .setSocketTimeout(10000).setConnectTimeout(10000).setRedirectsEnabled(true).build();
//        httpPost.setConfig(requestConfig);
//        CloseableHttpResponse response = null;
//        HttpEntity entity = null;
//        try {
//            response = client.execute(httpPost);
//            entity = response.getEntity();
//            html = EntityUtils.toString(entity, "utf-8");
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                EntityUtils.consume(entity);
//                response.close();
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    } catch (Exception e) {
//
//    }
//        try {
//        client.close();
//    } catch (Exception e) {
//
//    }

//    public static String getHtml() {
//
//        CloseableHttpClient client = HttpClients.custom().build();
//        String html = "";
//        try {
//            HttpPost httpPost = new HttpPost("http://23.228.89.165:808/curl.php?url=https%3A%2F%2Fwww.ncbi.nlm.nih.gov%2Fpubmed%2Fclinical");
//            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36");
//            List<NameValuePair> params = new LinkedList<>();
//            params.add(new BasicNameValuePair("isHistory", "false"));
//            params.add(new BasicNameValuePair("p$rq", "PubMedStaticPages.PubMedSP.ClinicalQueryC.ClinicalQuery:FetchClinicalStudy"));
//            params.add(new BasicNameValuePair("p$site", "pubmed"));
//            params.add(new BasicNameValuePair("cqTermComputed", "(hand) AND Medical Genetics[sb]"));
//            // 使用URL实体转换工具
//            UrlEncodedFormEntity urlEntity = null;
//            try {
//                urlEntity = new UrlEncodedFormEntity(params, "UTF-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//            httpPost.setEntity(urlEntity);
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(10000).setConnectTimeout(10000).setRedirectsEnabled(true).build();
//            httpPost.setConfig(requestConfig);
//            CloseableHttpResponse response = null;
//            HttpEntity entity = null;
//            try {
//                response = client.execute(httpPost);
//                entity = response.getEntity();
//                html = EntityUtils.toString(entity, "utf-8");
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                try {
//                    EntityUtils.consume(entity);
//                    response.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        } catch (Exception e) {
//
//        }
//        try {
//            client.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return html;
//    }

}
